<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>编辑数据上报模板</title>
	<#include "/common/resource.ftl">
	<style>
	fieldset{border: 1px solid #ddd;padding: 10px;}
	fieldset table{width: 100%;}
	fieldset table td{border-bottom: 1px solid #eee;padding: 5px;text-align: center;}
	</style>
    <script type="text/javascript">
        $(function () {
        <#if (params.id)??>
            $.ajaxRequest({
                url: '${params.contextPath}/web/reportTemplate/query.json',
                data: {id: "${params.id}"},
                success: function (data) {
                    if (!data.success) {
                        $.message(data.message);
                        return;
                    }
                    var record = data.data.template;
                    for (var key in record) {
                        $("[name='" + key + "']").val(record[key]);
                    }
                    var inputList = data.data.inputList;
                    if(inputList){
                    	var newTr = "";
                    	for(var i=0; i<inputList.length; i++){
                    		
                    		newTr = newTr  
                    			+'<tr class="input-item">'
                    			+'<td><input type="text" class="layui-input" placeholder="请输入录入项标签,可输入20个字符" pname="chName" value="'+inputList[i].chName+'" maxlength="20" /></td>'
                    			/*+'<td><input type="text" class="layui-input" placeholder="请输入录入项编码,可输入20个字符" pname="enName" value="'+inputList[i].enName+'" maxlength="20" /></td>'*/
                    			+'<td>'
                    			+'<select class="layui-selectnew" pname="validRule">'
                    		if(inputList[i].validRule=="not_null"){
                    			newTr = newTr 
                    			+'<option value="not_null" selected="true">必填</option>'
                    			+'<option value="integer">整数</option>'
                    			+'<option value="number">数值（含小数点）</option>';
                    		}
                    		if(inputList[i].validRule=="integer"){
                    			newTr = newTr 
                    			+'<option value="not_null">必填</option>'
                    			+'<option value="integer" selected="true">整数</option>'
                    			+'<option value="number">数值（含小数点）</option>';
                    		}
                    		if(inputList[i].validRule=="number"){
                    			newTr = newTr 
                    			+'<option value="not_null">必填</option>'
                    			+'<option value="integer">整数</option>'
                    			+'<option value="number" selected="true">数值（含小数点）</option>';
                    		}
                    		newTr = newTr 
                    			+'</select>'
                    			+'</td>'
                    			+'<td><span onclick="removeItem(this)">删除</span></td>'
                    			+'</tr>';
                    	}
                    	$("#items").append(newTr);
                    }
                }
            });
        </#if>
        });
    </script>
</head>
<body>
<div class="ui-form" style="padding: 20px;padding-top: 10px;">
    <form class="layui-form my-form" action="${params.contextPath}/web/reportTemplate/<#if (params.id)??>modify<#else>save</#if>.json" method="post">
        <input type="hidden" name="id" value="${params.id}"/>
        <input type="hidden" name="inputListStr"/>
        <fieldset>
        <legend>基本信息</legend>
        <div class="layui-form-item">
            <label class="layui-form-label">模板名称<span class="ui-request">*</span></label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" placeholder="请输入模板名称,可输入50个字符" name="name" maxlength="50">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">说明</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remark" placeholder="说明，可输入200个字符" maxlength="200" style="min-height: 60px;"></textarea>
            </div>
        </div>
        </fieldset>
        <fieldset style="margin-top: 10px;">
        <legend>模板录入项</legend>
        <table id="items">
        	<tr>
        		<td>录入项标签<span class="ui-request">*</span></td>
        		<#--<td>录入项编码(不可重复)<span class="ui-request">*</span></td>-->
        		<td>校验规则</td>
        		<td width="80px">操作</td>
        	</tr>
        	<#if (params.id)??>
        	
        	<#else>
        	<tr class="input-item">
        		<td><input type="text" class="layui-input" placeholder="请输入录入项标签,可输入20个字符" pname="chName" maxlength="20"/></td>
        		<#--<td><input type="text" class="layui-input" placeholder="请输入录入项编码,可输入20个字符" pname="enName" maxlength="20"/></td>-->
        		<td>
        			<select class="layui-selectnew" pname="validRule">
        				<option value="not_null">必填</option>
	                    <option value="integer">整数</option>
	                    <option value="number">数值（含小数点）</option>
        			</select>
        		</td>
        		<td><span onclick="removeItem(this)">删除</span></td>
        	</tr>
        	</#if>
        </table>
        <input type="button" value="增加一行" onclick="addItem()" class="layui-btn" style="margin-top: 5px;height: 25px;line-height: 25px;"/>
        </fieldset>
        <div class="layui-form-item" style="padding-top: 10px;text-align: center;">
            
                <input type="submit" value="保 存" class="layui-btn"/>
            
        </div>
    </form>
</div>
<script>

function addItem(){
	var newTr = ""  
	+'<tr class="input-item">'
	+'<td><input type="text" class="layui-input" placeholder="请输入录入项标签,可输入20个字符" pname="chName" maxlength="20" /></td>'
	/*+'<td><input type="text" class="layui-input" placeholder="请输入录入项编码,可输入20个字符" pname="enName" maxlength="20" /></td>'*/
	+'<td>'
	+'<select class="layui-selectnew" pname="validRule">'
	+'<option value="not_null">必填</option>'
	+'<option value="integer">整数</option>'
	+'<option value="number">数值（含小数点）</option>'
	+'</select>'
	+'</td>'
	+'<td><span onclick="removeItem(this)">删除</span></td>'
	+'</tr>';
	$("#items").append(newTr);
}
function removeItem(obj){
	//alert(1);
	$(obj).parent().parent().remove();
}

$(function(){
	$(".my-form").unbind().submit(function(){
		var inputList = [];
		$(".input-item").each(function(i,item){
			var chName = $(item).find("input[pname='chName']").val();
			/*var enName = $(item).find("input[pname='enName']").val();*/
			var validRule = $(item).find("select[pname='validRule']").val();
			var input = {chName:chName,enName:"",validRule:validRule};
			inputList.push(input);
		});
		$("input[name='inputListStr']").val(JSON.stringify(inputList));
        var form = $(this);
        form.formSubmit();
        return false;
    });
});
</script>
</body>
</html>
